package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.InheritCallBackBean;
import com.chuanjiabao.wisdom.databinding.ActivitySuccessorBinding;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.lzy.okgo.callback.StringCallback;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class SuccessorActivity extends BaseActivity {

    private ActivitySuccessorBinding binding;
    private InheritCallBackBean.DataBean dataBean;
    private List<InheritCallBackBean.DataListBean> list = new ArrayList<>();
    private SlimAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_successor);
        binding.setActivity(this);
        initView();

        list.addAll(dataBean.getHomes());
        mAdapter.updateData(list);
    }

    private void initView() {
        dataBean = (InheritCallBackBean.DataBean) getIntent().getSerializableExtra("data");

        binding.llModuleList.setLayoutManager(
                new LinearLayoutManager(
                        SuccessorActivity.this,
                        LinearLayoutManager.HORIZONTAL,
                        false)
        );
        binding.llModuleList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(
                    @NonNull Rect outRect,
                    @NonNull View view,
                    @NonNull RecyclerView parent,
                    @NonNull RecyclerView.State state
            ) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) != 0) outRect.left = -150;
            }
        });

        //noinspection Convert2Lambda
        mAdapter = SlimAdapter.create()
                .register(R.layout.item_successor_list2, new SlimInjector<InheritCallBackBean.DataListBean>() {
                    @Override
                    public void onInject(@NotNull InheritCallBackBean.DataListBean data, @NotNull IViewInjector injector) {
                        injector.with(
                                R.id.ivModuleRight,
                                view -> GlideUtil.setCircleMethod(SuccessorActivity.this, data.getAvatar(), (ImageView) view)
                        )
                                .with(R.id.llModuleRight, view -> {
                                    if (list.indexOf(data) % 2 == 0) {
                                        view.setBackgroundResource(R.drawable.module_str_family_left);
                                    } else {
                                        view.setBackgroundResource(R.drawable.module_str_family_right);
                                    }
                                });
                    }
                })
                .attachTo(binding.llModuleList);

        binding.llModuleFinish.setOnClickListener(this);
        binding.tvModuleOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llModuleFinish:
                finish();
                break;
            case R.id.tvModuleOk:
                getMerge();
                break;
        }
    }

    private void getMerge() {
        showProgressDialog();
        HTTPAPI.getInstance().getMergeInherit(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                cancelProgressDialog();
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(SuccessorActivity.this, s);
                } else {
                    Intent intent = getIntent();
                    intent.setClass(SuccessorActivity.this, SuccessorSelectActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

}
